$(function() {

	$.extend($.jgrid.defaults, {
		datatype : 'json',
		jsonReader : {
			repeatitems : false,
			total : function(result) {
				// Total number of pages
				return Math.ceil(result.total / result.max);
			},
			records : function(result) {
				// Total number of records
				return result.total;
			}
		},
		prmNames : {
			rows : 'max',
			search : null
		},
		height : 'auto',
		viewrecords : true,
		rowList : [ 10, 20, 50, 100 ],
		altRows : true,
		loadError : function(xhr, status, error) {
			alert(error);
		}
	});

	$.extend($.jgrid.edit, {
		closeAfterEdit : true,
		closeAfterAdd : true,
		ajaxEditOptions : {
			contentType : "application/json"
		},
		mtype : 'PUT',
		serializeEditData : function(data) {
			delete data.oper;
			return JSON.stringify(data);
		}
	});
	$.extend($.jgrid.del, {
		mtype : 'DELETE',
		serializeDelData : function() {
			return "";
		}
	});

	var editOptions = {
		onclickSubmit : function(params, postdata) {
			params.url = URL + '/' + postdata.id;
		}
	};
	var addOptions = {
		mtype : "POST"
	};
	var delOptions = {
		onclickSubmit : function(params, postdata) {
			params.url = URL + '/' + postdata;
		}
	};

	var URL = 'rest/book';
	var options = {
		url : URL,
		editurl : URL,
		colModel:[
{name:'id',index:'id', width:55,editable:false,editoptions:{readonly:true,size:10},hidden:true},		   		
		   		{name:'userName',index:'userName', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
		   		{name:'fullName',index:'fullName', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
		   		{	name:'secretPassword',index:'secretPassword',edittype:'password', 
		   			width:100,editable:true, editrules:{required:true}, 
		   			editoptions:{size:10}},
		   		{name:'birthday',index:'birthday', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
		   		{name:'email',index:'email', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
		   		{
		   			name:'state',
		   			index:'state', 
		   			formatter: 'checkbox',
				      width: 46,
				      align: 'center',
				      editable: true,
				      edittype: 'checkbox',
				      editoptions: {value:"true:false"}
		   		} ],
		caption : "Users",
		pager : '#pager',
		height : 'auto',
		ondblClickRow : function(id) {
			jQuery(this).jqGrid('editGridRow', id, editOptions);
		}
	};

	$("#grid").jqGrid(options).navGrid('#pager', {}, // options
	editOptions, addOptions, delOptions, {} // search options
	);

});